#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2025/9/17 15:05
# @Author  : Tom_zc
# @FileName: publish_community_transfer_obs.py.py
# @Software: PyCharm

import sys
import click
import logging
from pathlib import Path

CURR_DIR = Path(__file__).resolve()
ROOT_DIR = CURR_DIR.parent.parent.parent.parent
sys.path.append(ROOT_DIR.as_posix())

from scripts.utils.obs_helper import ObsHelper

logging.basicConfig(level=logging.INFO,
                    format="%(asctime)s  %(levelname)s:%(message)s")


class PipeLineLogHelper:

    def __init__(self, ak, sk, obs_endpoint):
        self._obs_helper = ObsHelper(ak, sk, obs_endpoint)

    def list_objects(self, bucket_name, prefix):
        files = self._obs_helper.list_objects(bucket_name, prefix)
        return files

    def transfer_objects(self, source_bk, source_key, dst_bk, dst_key):
        self._obs_helper.transfer_file(source_bk, source_key, dst_bk, dst_key)

    def work(self, from_obs_bucket_name, destination_bucket_name, prefix_path):
        file_path = self.list_objects(from_obs_bucket_name, prefix_path)
        for file in file_path:
            logging.info("start to move:{}".format(file["key"]))
            self.transfer_objects(from_obs_bucket_name, file, destination_bucket_name,
                                  file["key"][len(prefix_path) + 1:len(file["key"])])


@click.command()
@click.option("--ak")
@click.option("--sk")
@click.option("--obs_endpoint")
@click.option("--from_obs_bucket_name")
@click.option("--destination_bucket_name")
@click.option("--prefix_path")
def main(ak, sk, obs_endpoint, from_obs_bucket_name, destination_bucket_name, prefix_path):
    logging.info("=" * 20 + "start to transfer to obs" + "=" * 20)

    if not all([ak, sk, obs_endpoint, from_obs_bucket_name, destination_bucket_name, prefix_path]):
        logging.info("Lack of params, Please check.")
        sys.exit(-1)

    pipeline_log_helper = PipeLineLogHelper(ak, sk, obs_endpoint)
    pipeline_log_helper.work(from_obs_bucket_name, destination_bucket_name, prefix_path)

    logging.info("=" * 20 + "end to transfer the obs" + "=" * 20)


if __name__ == '__main__':
    main()
